package com.szqhyxj.youxiangju.biz.equipmentActivation;

import android.text.TextUtils;
import android.view.View;

import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard.HeadIdCardActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.my.MyModel;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.AliPayActivity;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.components.ChoiceDepositView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/3.
 */

public class EquipmentPresenter implements EquipmentContract.Presenter {
    EquipmentContract.View view;
    EquipmentModel.EquipmentEntity equipmentEntity;

    public EquipmentPresenter(EquipmentContract.View view) {
        this.view = view;
        equipmentEntity = new EquipmentModel.EquipmentEntity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void setdeviceNo(String s) {
        equipmentEntity.setDeviceNo(s);
        checkRegisterEnable();
    }

    @Override
    public void setprovCode(String s) {
        equipmentEntity.setProvCode(s);
        checkRegisterEnable();
    }

    @Override
    public void setcityCode(String s) {
        equipmentEntity.setCityCode(s);
        checkRegisterEnable();
    }

    @Override
    public void setareaCode(String s) {
        equipmentEntity.setAreaCode(s);
        checkRegisterEnable();
    }

    @Override
    public void setdetAddress(String s) {
        equipmentEntity.setDetAddress(s);
        checkRegisterEnable();
    }

    private void checkRegisterEnable() {
        if (equipmentEntity.valid()) {
            view.showCommit();

        } else {
            view.enshowCommit();

        }
    }

    @Override
    public void devicOrderPay(String deviceNo) {

        MyModel.deviceOrderPay(deviceNo)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<String>>() {
                    @Override
                    public void accept(BaseResult<String> monthProfit) throws Exception {
                        if (monthProfit.businessLogicSuccess()) {
                            //成功 0000
                            String payInfo = monthProfit.getData();
                            if (TextUtils.isEmpty(payInfo)) {
                                return;
                            }
                            ChoiceDepositView choicedepositview = new ChoiceDepositView(view.getCurruntActivity());
                            choicedepositview.show();
                            choicedepositview.setOnClickListener(new ChoiceDepositView.OnClickListener() {
                                @Override
                                public void onClickConfirm(View v) {
                                    AliPayActivity.start(view.getCurruntActivity(),payInfo,AliPayActivity.DISPOSE_SUCESS);
                                    choicedepositview.dismiss();
                                }
                            });

                        } else if (monthProfit.getCode().equals("10003")) {//设备已支付押金
                            activation();
                        } else {
                            view.showToast(monthProfit.getMessage() + "");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void commit() {
    }

    public void activation() {
        view.showLoading("正在激活...");
        EquipmentModel.getmerchntNet(this.equipmentEntity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        view.dismissLoading();
                        if (baseResult.businessLogicSuccess()) {
                            view.commitSuccess();
                            getUserInfo();
                        } else {
                            view.commitFail(baseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.commitFail(throwable.getMessage());
                    }
                });
    }

    public void getUserInfo() {

        UserInfoModel.loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<UserInfo>>() {
                            @Override
                            public void accept(BaseResult<UserInfo> baseResult) throws Exception {
                                if (baseResult.businessLogicSuccess()) {
                                    UserInfoManger.INSTANCE.clear();
                                    UserInfoManger.INSTANCE.init(baseResult.getData());
                                } else {
                                    view.showToast(baseResult.getMessage());
                                }
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                view.showToast(throwable.getMessage());
                            }
                        });
    }
}
